package com.jasonjan.jiance.banner;

import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.widget.ImageView;

import com.jasonjan.jiance.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;

/**
 * ImageView创建工厂
 */
public class ViewFactory {

	/**
	 * 获取ImageView视图的同时加载显示url
	 * 
	 * @param
	 * @returnuniversal-image-loader-1.9.2-SNAPSHOT-with-sources.jar
	 */
	public static ImageView getImageView(Context context, String url) {
		initImageLoader(context);
		ImageView imageView = (ImageView) LayoutInflater.from(context).inflate(
				R.layout.view_banner, null);
		ImageLoader.getInstance().displayImage(url, imageView);//最核心的部分
		return imageView;
	}

	/** 初始化imageLoader */
	private static void initImageLoader(Context context) {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.mipmap.icon_empty)
				.showImageOnFail(R.mipmap.icon_empty).showImageOnLoading(R.mipmap.icon_empty).cacheInMemory(true)
				.cacheOnDisc(true).build();

		File cacheDir = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "jiance" + File.separator + "Images"+File.separator);
		ImageLoaderConfiguration imageconfig = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheSize(50 * 1024 * 1024)
				.discCacheFileCount(200)
				.discCache(new UnlimitedDiscCache(cacheDir))
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.defaultDisplayImageOptions(options).build();

		ImageLoader.getInstance().init(imageconfig);

	}
}
